package com.itheima.run;

import com.itheima.Utils.Utils;
import com.itheima.domain.Student;

import java.util.ArrayList;
import java.util.Scanner;

//学生管理类
public class StudentManager {

    public static void studentmanager(ArrayList<Student> list) {
        while (true) {
            System.out.println("=====================================================");
            System.out.println("【学员信息管理】");
            System.out.println();
            System.out.println("1.添加学员  2.修改学员  3.查询学员  4.删除学员  5.返回");
            System.out.println("=====================================================");
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入菜单编号");
            int i = sc.nextInt();
            switch (i) {
                case 1:
                    addstudent(list);
                    break;
                case 2:
                    setstudent(list);
                    break;
                case 3:
                    findstudent(list);
                    break;
                case 4:
                    removestudent(list);
                    break;
                case 5:
                    return;
                default:
                    System.out.println("您输入的有误!");
            }
        }
    }

    //增加学生;
    public static void addstudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入学生姓名:");
        String name = sc.next();
        System.out.println("请输入学生性别:");
        String sex = sc.next();
        System.out.println("请输入学生生日(格式:2000-01-01):");
        String birtnday = sc.next();
        int ID = ++Utils.stuId;
        int age = Utils.birthdayToAge(birtnday);
        Student stu = new Student(ID, name, sex, birtnday, age);
        list.add(stu);
        System.out.println("信息添加成功.");
    }

    //查看学生方法;
    public static void findstudent(ArrayList<Student> list) {
        if (list.size() == 0) {
            System.out.println("没有你要查找的信息.");
        } else {
            Utils.printPersonList(list);
        }
    }

    //删除学生方法;
    public static void removestudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要删除的学生编号:");
        int id = sc.nextInt();
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            int id1 = stu.getId();
            if (id == id1) {
                list.remove(i);
                System.out.println("删除成功.");
                return;
            }
        }
        System.out.println("没有你要查找的信息.");
    }

    //修改学生的方法;
    public static void setstudent(ArrayList<Student> list) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入你要修改的学生编号:");
        int id = sc.nextInt();
        for (int i = 0; i < list.size(); i++) {
            Student student = list.get(i);
            int id1 = student.getId();
            if (id == id1) {
                System.out.println("请输入新的学生名称(不变输入0):");
                String name = sc.next();
                System.out.println("请输入新的学生性别(不变输入0):");
                String sex = sc.next();
                System.out.println("请输入新的学生生日(不变输入0):");
                String birthday = sc.next();
                if (!name.equals("0")) {
                    student.setName(name);
                }
                if (!sex.equals("0")) {
                    student.setSex(sex);
                }
                if (!birthday.equals("0")) {
                    student.setBirthday(birthday);
                    int age = Utils.birthdayToAge(birthday);
                    student.setAge(age);
                }
                System.out.println("修改成功");
                return;
            }
        }
        System.out.println("没有你要查找的编号.");

    }
}
